package com.example.demo01.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * PG工具接口
 */
public class JSONPGUtils {

    static String jsonFilePath="src/main/resources/static/temp.json";//缓冲区json文件
    static String heatMapJsonFilePath="src/main/resources/static/heatmaparray.json";//热力图json文件


    /**
     * 获取焦作市的边界坐标数组
     * @return
     */
    public static List<Float> getJiaoZuoCoordsArrayString(){
        //结果集
        List<Float> resList=new ArrayList<>();
        String sql="select st_asgeojson(geom) as json from jiaozuopolygon";
        //声明变量
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        //数据库操作
        try {
            connection=JDBCPGUtils.getConnection();
            statement=connection.prepareStatement(sql);
            resultSet=statement.executeQuery();
            if (resultSet.next()){
                String jsonStr=resultSet.getString("json");
                resList.addAll(getCoordsArrayByJsonStr(jsonStr));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCPGUtils.close(resultSet,statement,connection);
        }
        return resList;
    }


    /**
     * 根据获取每一个行政区划的范围边界数组
     * @param cityName 城市名称
     * @return 城市边界坐标数组
     */
    public static List<Float> getCityBoundsArray(String cityName){
        List<Float> coordsArray=new ArrayList<>();
        //SQL语句
        String sql="select st_asgeojson(jiaozuo.geom) as json from jiaozuo where name=?";
        //声明变量
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        //数据库操作
        try {
            connection= JDBCPGUtils.getConnection();
            statement=connection.prepareStatement(sql);
            //设置参数
            statement.setString(1,cityName);
            //获取查询结果
            resultSet=statement.executeQuery();
            //解析查询结果
            if (resultSet.next()){
                String resultJson=resultSet.getString("json");
                //解析字符串
                 coordsArray.addAll(getCoordsArrayByJsonStr(resultJson)) ;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCPGUtils.close(resultSet,statement,connection);
        }
        return coordsArray;
    }

    /**
     * 根据json字符串获取对应的坐标数组
     * @param jsonstr json字符串
     * @return 坐标数组
     */
    public static List<Float> getCoordsArrayByJsonStr(String jsonstr){
//        String jsonstr="{\"type\":\"MultiPolygon\",\"coordinates\":[[[[113.00202901,35.3590237],[113.00225332,35.359008485],[113.00260479,35.359025655],[113.00295795,35.35911183],[113.00336927,35.35927756],[113.00362563,35.359422875],[113.003926095,35.359647955],[113.00430126,35.360055835],[113.00446333,35.360363745],[113.004934975,35.36126471],[113.00519605,35.36216914],[113.00529915,35.36236298],[113.00544093,35.362418145],[113.005624835,35.362472605],[113.00587808,35.362491425],[113.00648054,35.36243539],[113.00722079,35.362273575],[113.01209236,35.35988909],[113.01326322,35.358948065],[113.014445855,35.35747144],[113.01643834,35.35698936],[113.01818505,35.356566705],[113.02238046,35.358074165],[113.02930147,35.36038329],[113.029404115,35.360378005],[113.029577555,35.360366795],[113.030030275,35.36029278],[113.030293355,35.360219575],[113.032695845,35.35950084],[113.03766117,35.357710665],[113.043050885,35.35530713],[113.04786907,35.353326925],[113.051794555,35.352745635],[113.051939675,35.352702855],[113.053569025,35.35199446],[113.0539278,35.351815225],[113.055431015,35.351000005],[113.055975385,35.350704785],[113.05610995,35.35053172],[113.05719195,35.348741385],[113.06322031,35.353088485],[113.063800655,35.353255725],[113.064230535,35.353114405],[113.0675862,35.34930943],[113.07489685,35.343242655],[113.08014312,35.339682825],[113.08041042,35.33950496],[113.081007705,35.339238925],[113.084999515,35.33769811],[113.08512805,35.337648495],[113.088158915,35.336503355],[113.088396575,35.336464735],[113.08860705,35.33646111],[113.08890348,35.336525005],[113.09045252,35.337257415],[113.09104659,35.337431195],[113.09195145,35.337680115],[113.0932312,35.338011965],[113.093406165,35.338057335],[113.094113775,35.338240825],[113.094423175,35.33831068],[113.09522852,35.33846308],[113.09812714,35.338723375],[113.098619165,35.338749355],[113.09908031,35.33870658],[113.09937541,35.3386787],[113.10076012,35.338482075],[113.10145934,35.33837791],[113.10202987,35.33818393],[113.104906135,35.33704104],[113.109193525,35.33510278],[113.10973358,35.334817265],[113.110743505,35.334247475],[113.112371975,35.33333378],[113.116530665,35.33114874],[113.117322155,35.330786935],[113.11769589,35.33062828],[113.1179642,35.33062075],[113.119427155,35.33083703],[113.120784115,35.331066285],[113.12868348,35.334138855],[113.12916185,35.3344328],[113.13109173,35.336341025],[113.131523935,35.336768365],[113.131591835,35.33500441],[113.131653945,35.333390955],[113.131675225,35.332838135],[113.13081248,35.33151741],[113.129233075,35.32950953],[113.12896943,35.329323325],[113.12475174,35.323396415],[113.122779425,35.320554395],[113.118863855,35.314911555],[113.11876969,35.31427704],[113.117524535,35.30918833],[113.1174143,35.30874195],[113.11503752,35.29911682],[113.11487081,35.29867441],[113.11467658,35.298168875],[113.11445707,35.29768499],[113.107763725,35.286944585],[113.10257768,35.279422045],[113.098349325,35.27690839],[113.09793302,35.276809585],[113.09756514,35.276582705],[113.097272615,35.27626969],[113.097108775,35.275933235],[113.094913205,35.27100915],[113.094595055,35.2696999],[113.094535715,35.269404055],[113.092583465,35.26407841],[113.090170165,35.25749407],[113.090086755,35.257266475],[113.089848735,35.257058535],[113.089481505,35.25685282],[113.088855875,35.25665157],[113.08823078,35.256471505],[113.08655108,35.256500495],[113.086187665,35.25644315],[113.086106335,35.256296105],[113.0861009,35.25608414],[113.087396595,35.25116324],[113.087518185,35.250864265],[113.089015545,35.24776356],[113.089315815,35.247376675],[113.08967266,35.247179655],[113.09447535,35.246969365],[113.095071275,35.24702266],[113.09559186,35.24716209],[113.09793661,35.246866975],[113.098589215,35.245816845],[113.10031,35.243047725],[113.102267915,35.239896755],[113.1032333,35.238343005],[113.10446363,35.23636276],[113.107937715,35.233778795],[113.1076357,35.231865015],[113.107286655,35.22965312],[113.10643339,35.221879745],[113.106400765,35.217137245],[113.1065348,35.216292855],[113.10663284,35.215675215],[113.107513435,35.214072705],[113.11379108,35.204449395],[113.118552025,35.198594185],[113.128462075,35.190350525],[113.12906066,35.190247035],[113.12899583,35.186245275],[113.128811025,35.183105605],[113.12604219,35.18257529],[113.1264091,35.17996113],[113.125829215,35.179766255],[113.12261031,35.178712145],[113.11946955,35.17823088],[113.119697375,35.173890755],[113.11723528,35.17367922],[113.117207725,35.17144504],[113.115990475,35.171357615],[113.116044055,35.170026605],[113.11288328,35.169968465],[113.112921425,35.169311895],[113.11120761,35.169286205],[113.11117579,35.170357625],[113.1086027,35.170290165],[113.10864232,35.169250515],[113.10744599,35.16923784],[113.10744439,35.16912785],[113.10748212,35.166027665],[113.109032055,35.165956565],[113.109068855,35.159046825],[113.108042425,35.15906481],[113.10800089,35.157816485],[113.10617848,35.157803495],[113.106199785,35.15680376],[113.10488205,35.157086995],[113.10473369,35.15587288],[113.104168235,35.15587423],[113.10417143,35.154791155],[113.103303915,35.15480218],[113.102584775,35.147319385],[113.09852836,35.147137935],[113.09843903,35.144526605],[113.09408652,35.144504765],[113.094053195,35.143079875],[113.091373505,35.14278478],[113.091598195,35.14069957],[113.08740193,35.14049276],[113.087061295,35.134471885],[113.09307237,35.134229325],[113.093471355,35.12605579],[113.096617345,35.126114045],[113.09698506,35.123200505],[113.110713615,35.12346578],[113.110745665,35.12255029],[113.124377265,35.12308764],[113.124716125,35.12164653],[113.12919981,35.12174786],[113.12923394,35.12057424],[113.13143069,35.120599015],[113.131538795,35.11836919],[113.137833125,35.11860087],[113.137884175,35.11640009],[113.146236305,35.11721912],[113.146330515,35.115434295],[113.148520295,35.115505105],[113.148716565,35.112401385],[113.1457462,35.112309215],[113.146162945,35.103584225],[113.149595245,35.10379258],[113.14988248,35.100648935],[113.159905415,35.100665815],[113.164798685,35.100877315],[113.16516591,35.0989569],[113.166208425,35.09868069],[113.167130005,35.089959],[113.169666395,35.08990701],[113.170671395,35.08048882],[113.16899494,35.079130625],[113.16570601,35.077536105],[113.16347553,35.076661035],[113.163477275,35.07664999],[113.164106485,35.071245655],[113.1635039,35.066695115],[113.159200905,35.06633553],[113.15540844,35.063317605],[113.151400515,35.05770855],[113.14995573,35.051795345],[113.14977945,35.051073815],[113.15036329,35.045249205],[113.14815586,35.044742075],[113.14257314,35.04287098],[113.141935165,35.042387355],[113.141230375,35.0416041],[113.14074877,35.04123415],[113.13765689,35.039852535],[113.13085219,35.03724552],[113.129625445,35.03680129],[113.12845919,35.0364142],[113.12261374,35.036206125],[113.12145955,35.036284535],[113.11909246,35.03645207],[113.11744597,35.03665549],[113.1169213,35.03689754],[113.115168335,35.038005245],[113.11418517,35.038769585],[113.113664975,35.039186205],[113.11339317,35.039627615],[113.113197635,35.04028118],[113.112686665,35.041056685],[113.11228313,35.041422755],[113.108119775,35.04424137],[113.10722409,35.04474213],[113.103045185,35.046047055],[113.102363845,35.046165615],[113.101644385,35.046178085],[113.09733151,35.045942225],[113.09438828,35.045740785],[113.093607125,35.04564753],[113.08063177,35.04394947],[113.071821865,35.042246855],[113.071286075,35.04213086],[113.069834885,35.04181669],[113.06625085,35.041040695],[113.06227124,35.040178895],[113.06139372,35.039988865],[113.058458385,35.03868017],[113.05746237,35.038483585],[113.056174445,35.03840839],[113.050767885,35.03829614],[113.049625125,35.03836398],[113.046067905,35.038739835],[113.045429365,35.038807305],[113.044423385,35.038913575],[113.042586045,35.03947313],[113.041998055,35.03952232],[113.035173765,35.039868845],[113.03149526,35.039979035],[113.029770215,35.03988177],[113.029360485,35.03976909],[113.02860602,35.03956161],[113.02701264,35.039539725],[113.026309555,35.03973585],[113.025752775,35.040123605],[113.0250164,35.040883115],[113.02491341,35.041034275],[113.02470991,35.041332965],[113.0245661,35.041575605],[113.02436202,35.04209733],[113.02428537,35.0428167],[113.02443993,35.046210515],[113.024634495,35.049312545],[113.024781255,35.04999908],[113.024508985,35.051876485],[113.02443565,35.052093135],[113.023941605,35.053552585],[113.02360709,35.05454073],[113.02327696,35.05503144],[113.01837241,35.05857749],[113.01813092,35.058697715],[113.017626255,35.058948955],[113.010494985,35.062192085],[113.00641981,35.063905875],[113.005798855,35.064167],[113.005345955,35.06422786],[113.005325715,35.06422393],[113.004563275,35.06407584],[113.003243285,35.063712065],[113.00237258,35.06374255],[112.99831958,35.06395328],[112.997433385,35.0642273],[112.997055895,35.064580725],[112.99701395,35.06533536],[112.997162085,35.06596782],[112.997359675,35.066649065],[112.99773045,35.06701001],[112.99818613,35.06738939],[112.99931511,35.068164405],[113.00023063,35.06909173],[113.002041415,35.07172966],[113.002286115,35.07209705],[113.002316965,35.07235493],[113.00226252,35.072739375],[113.00197662,35.073162365],[113.00126965,35.07379744],[113.000683165,35.07430127],[112.999394045,35.07536852],[112.99733038,35.076625865],[112.9875505,35.082583545],[112.984336605,35.085666855],[112.983300105,35.086472475],[112.9807545,35.08800711],[112.98007294,35.08827121],[112.976541825,35.08898358],[112.97534649,35.08896339],[112.97482392,35.088939215],[112.97481562,35.08893883],[112.97457215,35.08913473],[112.97410093,35.08961362],[112.974037775,35.08977001],[112.97367951,35.09065715],[112.97203042,35.096353445],[112.971858335,35.097047765],[112.97546906,35.097250455],[112.97558824,35.09736803],[112.97559344,35.09758355],[112.97026863,35.10957395],[112.95979375,35.11212454],[112.96012511,35.11471067],[112.960794935,35.11961286],[112.961094155,35.12151767],[112.961187575,35.12201466],[112.96149267,35.123408475],[112.961588965,35.12377397],[112.961825895,35.124316775],[112.963401665,35.126466],[112.96395883,35.1302816],[112.964027515,35.13192986],[112.96394485,35.13212243],[112.962566495,35.132885785],[112.961754365,35.13299456],[112.96143442,35.13299974],[112.96128497,35.132834835],[112.961269495,35.13218967],[112.96119642,35.13156936],[112.960777625,35.12987896],[112.9605415,35.129739355],[112.960307675,35.12969533],[112.954965675,35.130188015],[112.950949095,35.13146229],[112.950553255,35.13186992],[112.950216995,35.1322162],[112.94800201,35.134597985],[112.947861755,35.13479369],[112.947674375,35.135101745],[112.9475144,35.135483775],[112.947372305,35.13585559],[112.944471345,35.145273],[112.943777965,35.14915657],[112.943403525,35.15176811],[112.943582425,35.15439467],[112.945477255,35.15940798],[112.94562785,35.1596207],[112.949987135,35.16421185],[112.950464685,35.16470614],[112.9509371,35.164985375],[112.951667405,35.165093135],[112.952133545,35.16510952],[112.953005915,35.165071555],[112.95425141,35.16481242],[112.9554159,35.164817525],[112.956843985,35.16489007],[112.957573145,35.164950005],[112.958486075,35.16538941],[112.959819785,35.166179515],[112.96548181,35.1697139],[112.96642147,35.170369765],[112.967291585,35.1710851],[112.967582055,35.171342985],[112.96931279,35.173853405],[112.96973521,35.175159585],[112.969920565,35.17547753],[112.97109253,35.1769174],[112.97254309,35.17811928],[112.980456005,35.18347571],[112.981106105,35.18390276],[112.984235885,35.18691525],[112.98794481,35.191135705],[112.99002251,35.193951165],[112.99016028,35.195218415],[112.99019965,35.195681055],[112.9900162,35.196527095],[112.990024445,35.196865025],[112.990029415,35.19706866],[112.990049585,35.197118675],[112.99031355,35.19777314],[112.990623105,35.198256845],[112.991676605,35.19935319],[112.99266222,35.20007139],[112.99299284,35.200260055],[112.993707395,35.20066781],[112.994097695,35.200890525],[112.99447142,35.201075435],[112.99560804,35.201637785],[112.997653775,35.20264988],[112.99780174,35.202711645],[113.000691885,35.20391804],[113.001272225,35.204419395],[113.00180106,35.2050622],[113.00493993,35.20906724],[113.005507875,35.210257745],[113.006259965,35.213964455],[113.00515305,35.216034505],[113.00332951,35.21944448],[113.002523395,35.219620765],[112.999058485,35.21897667],[112.998192345,35.218815645],[112.997838785,35.21861341],[112.997610655,35.21835709],[112.995605035,35.2153992],[112.99550491,35.21519278],[112.99556126,35.214905765],[112.995886795,35.21396409],[112.995843655,35.213496645],[112.99574227,35.21323822],[112.99532219,35.21290703],[112.994102575,35.21227687],[112.99241036,35.21175852],[112.991230065,35.21143981],[112.984507075,35.20993769],[112.980063585,35.209646365],[112.976249055,35.21046293],[112.9749883,35.21074358],[112.97445924,35.21114233],[112.97430778,35.21143088],[112.974257025,35.211951865],[112.974377865,35.2130162],[112.974607795,35.21335055],[112.97486437,35.213476395],[112.97540162,35.21341562],[112.975966785,35.213198335],[112.976965335,35.212557845],[112.977686335,35.21223397],[112.978345245,35.211963125],[112.9789129,35.21184982],[112.97954834,35.211917455],[112.980124835,35.21216809],[112.980447925,35.212422885],[112.98054863,35.21265531],[112.981565135,35.21670926],[112.982449825,35.22023718],[112.98353281,35.224657575],[112.983539085,35.225072975],[112.98352608,35.22526813],[112.98343331,35.225482465],[112.982233255,35.22661589],[112.981717995,35.226862275],[112.97958379,35.22772945],[112.98074391,35.229332225],[112.98094524,35.229610365],[112.98276523,35.23212463],[112.98681418,35.233421955],[112.98990266,35.234946305],[112.99019609,35.235279575],[112.991859215,35.23847712],[112.991763795,35.24244746],[112.99174114,35.243390115],[112.99170524,35.244883685],[112.991667165,35.246467875],[112.991607835,35.24893623],[112.988378635,35.25172012],[112.985993765,35.25303277],[112.98409255,35.254079145],[112.983394205,35.25457665],[112.982340255,35.255327465],[112.979946205,35.25703286],[112.976581375,35.25942959],[112.979618625,35.26282461],[112.980757125,35.264097125],[112.981879125,35.265265975],[112.98397462,35.267448855],[112.98426893,35.267027925],[112.988757765,35.26060731],[112.98903917,35.26030091],[112.98905545,35.26028319],[112.989116745,35.26027941],[112.989369215,35.26026384],[112.989451335,35.26027161],[112.989773775,35.26030211],[112.990105375,35.26042199],[112.99062159,35.260664145],[112.9910166,35.2609012],[112.99108206,35.26098842],[112.991284815,35.26125858],[112.998331355,35.271232225],[112.99866629,35.271706215],[112.99956373,35.273383395],[112.99978781,35.274764735],[112.999838045,35.275520085],[112.99990657,35.27572723],[113.001601925,35.277593095],[113.002102475,35.2780746],[113.00303888,35.278615675],[113.003533955,35.278874635],[113.004163085,35.27906458],[113.005118975,35.279293625],[113.007661555,35.280008385],[113.008319485,35.28026462],[113.00934123,35.280960075],[113.00962142,35.28131163],[113.009771425,35.28188798],[113.012047975,35.292692295],[113.01437577,35.29765514],[113.01586115,35.300821615],[113.01526229,35.30076481],[113.010447585,35.300824085],[113.01013117,35.30082797],[113.00357251,35.303190405],[113.00280946,35.30346522],[113.00265042,35.303623705],[113.0026392,35.30368669],[113.003331065,35.306996425],[113.00407101,35.31056853],[113.00399616,35.31083693],[113.00356646,35.31213532],[113.003436205,35.31236011],[113.00303609,35.312656155],[113.00274019,35.312772375],[113.00246868,35.312776875],[112.998744,35.31187],[112.996610705,35.31075024],[112.99589193,35.31037294],[112.994003235,35.30943843],[112.993679525,35.30944376],[112.990461265,35.31114985],[112.987431,35.31275614],[112.987355105,35.31326417],[112.987133835,35.31384697],[112.986994365,35.314162985],[112.986503485,35.31455716],[112.98577423,35.3148346],[112.98456701,35.31483029],[112.98373943,35.31469908],[112.982851235,35.31449646],[112.98142152,35.313820085],[112.981342265,35.313761575],[112.98094182,35.31346596],[112.98051922,35.313038515],[112.98023965,35.312439785],[112.98007956,35.31191151],[112.97989622,35.31124759],[112.979855485,35.31106352],[112.97981977,35.311081455],[112.97916796,35.31140882],[112.97906723,35.311466285],[112.978865955,35.311603295],[112.97879555,35.311669565],[112.978688095,35.31180037],[112.9785848,35.311933875],[112.97852328,35.312022845],[112.97846913,35.31213334],[112.97844686,35.3122055],[112.978437315,35.312311675],[112.978448225,35.3124314],[112.97905063,35.31904358],[112.97898463,35.321059485],[112.978932985,35.322636855],[112.97892761,35.322800965],[112.978863675,35.32475377],[112.978845775,35.32530041],[112.97860647,35.332608585],[112.9784682,35.33328635],[112.978156145,35.33481594],[112.97807311,35.33522295],[112.977940215,35.335874335],[112.978196425,35.33630409],[112.982651025,35.343775225],[112.985123915,35.35108131],[112.98475526,35.351761775],[112.98436473,35.35253036],[112.98402003,35.353208755],[112.983915535,35.35358492],[112.98321514,35.35610612],[112.983217595,35.35620652],[112.98329219,35.356391835],[112.983568475,35.356946925],[112.98373168,35.35717383],[112.98394808,35.357428555],[112.989740225,35.36291506],[112.990152435,35.363295715],[112.99048971,35.363476685],[112.990753745,35.363529725],[112.99103426,35.36353945],[112.992435035,35.363516355],[112.99259262,35.36351376],[112.99280133,35.36345292],[112.99320123,35.363331525],[112.995146445,35.362653715],[112.995597085,35.36245974],[112.99604704,35.362237085],[112.996512025,35.36191371],[112.99697735,35.3616047],[112.997649615,35.361163125],[113.00056164,35.359352935],[113.001398215,35.35906865],[113.00202901,35.3590237]]]]}";
        JSONObject jsonObject = JSONObject.parseObject(jsonstr);
//        System.out.println(jsonObject);
        String coordinates = jsonObject.get("coordinates").toString();
        //替换字符
        coordinates=coordinates.replace("["," ");
        coordinates=coordinates.replace("]"," ");
        //字符串分割
        String[] split = coordinates.split(",");
        //获取结果集
        List<Float> coordsArray=new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            coordsArray.add(Float.parseFloat(split[i].trim()));
        }
        return coordsArray;
    }


    /**
     * 4326坐标系下的json格式的文件
     * @param lon 经度
     * @param lat 纬度
     *  @return 输出为4326坐标系下的json格式的文件
     */
    public static boolean getBufferJSONByLonLat(String lon,String lat,String distance){
        //SQL语句---返回
        String sql="select st_asgeojson(ST_Transform(ST_Buffer(ST_Transform(ST_SetSRID(ST_GeomFromText('POINT("+lon+" "+lat+")'),4326),3857),"+distance+"),4326))  as json";
        //声明变量
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        //结果
        String jsonResult="";
        //数据库操作
        try {
            connection=JDBCPGUtils.getConnection();
            statement=connection.prepareStatement(sql);
            resultSet=statement.executeQuery();
            if (resultSet.next())
            {
                jsonResult=resultSet.getString("json");
                System.out.println(jsonResult);
                //将内容保存到到temp.json文件中
                if (saveAsJsonFile(jsonResult,jsonFilePath))
                    return true;
                return false;
            }else
                return false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            JDBCPGUtils.close(resultSet,statement,connection);
        }
    }

    /**
     * 将json文件内容保存到文件中
     * @param json
     * @return 布尔值：true-写入成功;false-写入失败
     */
    public static boolean saveAsJsonFile(String json,String jsonFilePath){
        System.out.println("更新json文件");
        File file=new File(jsonFilePath);
        BufferedWriter bufferedWriter=null;
        //异常处理
        try {
            //判断文件是否存在
            if (!file.exists())
                //如果文件不存在，就创建它
                file.createNewFile();
            //以字符流形式写入文件内容
            bufferedWriter=new BufferedWriter(new FileWriter(file));
            //写入内容
            bufferedWriter.write(json,0,json.length());
            //刷新缓冲区
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
            if (bufferedWriter!=null)
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return true;
    }

    /**
     * 更新热力图json文件数据源
     * @return
     */
    public static boolean getHeatMapArrayJsonInJiaoZuo(){
        //SQL语句
        String sql="select st_asgeojson(geom) as json from jiaozuopolygon where name='焦作市'";
        //声明变量
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        //数据库操作
        try {
            connection=JDBCPGUtils.getConnection();
            statement=connection.prepareStatement(sql);
            resultSet=statement.executeQuery();
            if (resultSet.next()){
                String formatData= getFormatHeatMapArrayJsonContent(resultSet.getString("json"));
                //将文件内容写入到heatmaparray.json文件中
                return saveAsJsonFile(formatData,heatMapJsonFilePath);
            }else{
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean getHeatMapArrayJsonByDomainName(String domain){
        //SQL语句
        String sql="select st_asgeojson(geom) as json from jiaozuo where name=?";
        //声明变量
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        //数据库操作
        try {
            connection=JDBCPGUtils.getConnection();
            statement=connection.prepareStatement(sql);
            statement.setString(1,domain);
            resultSet=statement.executeQuery();
            if (resultSet.next()){
               String formatData= getFormatHeatMapArrayJsonContent(resultSet.getString("json"));
               //将文件内容写入到heatmaparray.json文件中
               return saveAsJsonFile(formatData,heatMapJsonFilePath);
            }else{
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据geojson内容获取格式化的经纬度坐标数组
     * @param geojson
     * @return 格式化的经纬度坐标数组[lon1,lat2,lon2,lat2]
     */
    public static String getFormatHeatMapArrayJsonContent(String geojson){
        JSONObject jsonObject = JSON.parseObject(geojson);
        //解析jsonObject
        String type = jsonObject.get("type").toString();//获取类型:MultiPolygon
        String coordinates=jsonObject.get("coordinates").toString();//获取坐标信息：coordinates
        return JSONArray.parse("["+coordinates.replace("[","").replace("]","")+"]").toString();
    }

    /**
     * 读取heatmaparray.json文件的内容
     */
    public static String getHeatMapJsonFileContent(){
        File file=new File(heatMapJsonFilePath);
        if (!file.exists())
            return "";
        BufferedReader bufferedReader=null;
        String result="";
        //如果文件存在，读取文件内容
        try {
            bufferedReader=new BufferedReader(new FileReader(file));
            //读取文件内容
            char[] ch=new char[1024];
            int pos=-1;
            while ((pos=bufferedReader.read(ch))!=-1){
                result+=new String(ch,0,pos);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //资源释放
            if (bufferedReader!=null)
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return result;
    }


    @Test
    public void test(){
        //F:\myNotes\Java\JavaWeb\Spring Boot\demoes\demo01\src\main\resources\static\jsonresources
        String json="{\"type\":\"Polygon\",\"coordinates\":[[[113.198956306,35.2248],[113.198611088,35.221936689],[113.197588702,35.219183317],[113.195928437,35.216645713],[113.193694097,35.214421419],[113.190971545,35.212595943],[113.187865408,35.211239465],[113.184495052,35.210404139],[113.18099,35.210122082],[113.177484948,35.210404139],[113.174114592,35.211239465],[113.171008455,35.212595943],[113.168285903,35.214421419],[113.166051563,35.216645713],[113.164391298,35.219183317],[113.163368912,35.221936689],[113.163023694,35.2248],[113.163368912,35.22766321],[113.164391298,35.230416294],[113.166051563,35.232953467],[113.168285903,35.235177253],[113.171008455,35.237002222],[113.174114592,35.238358269],[113.177484948,35.239193308],[113.18099,35.239475264],[113.184495052,35.239193308],[113.187865408,35.238358269],[113.190971545,35.237002222],[113.193694097,35.235177253],[113.195928437,35.232953467],[113.197588702,35.230416294],[113.198611088,35.22766321],[113.198956306,35.2248]]]}";
        File file=new File(jsonFilePath);
        BufferedWriter bufferedWriter=null;
        //异常处理
        try {
            //判断文件是否存在
            if (!file.exists())
                //如果文件不存在，就创建它
                file.createNewFile();
            //以字符流形式写入文件内容
            bufferedWriter=new BufferedWriter(new FileWriter(file));
            //写入内容
            bufferedWriter.write(json,0,json.length());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bufferedWriter!=null)
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

    @Test
    public void test1(){
        String str="{\"type\":\"MultiPolygon\",\"coordinates\":[[[[113.205977195,35.31361327],[113.202159025,35.31235129],[113.20081792,35.31173549],[113.199540945,35.310979325],[113.18700933,35.30338484],[113.186593065,35.303058335],[113.178124275,35.294943945],[113.177423185,35.294121495],[113.17695892,35.293266935],[113.176839225,35.292601025],[113.176892195,35.292043355],[113.177096195,35.29078706],[113.177846895,35.287127015],[113.1783723,35.285196865],[113.17842525,35.284639195],[113.178326825,35.28349969],[113.178176935,35.2829735],[113.177846965,35.282060855],[113.177421915,35.28140045],[113.17676159,35.28082778],[113.174336625,35.2790064],[113.172507885,35.27789798],[113.17092264,35.27700787],[113.168066495,35.275555955],[113.1650091,35.27419109],[113.163454235,35.273161145],[113.16300134,35.272723865],[113.16278149,35.272115405],[113.162760035,35.271308535],[113.163660275,35.268146945],[113.164317185,35.26604747],[113.16463199,35.265123245],[113.168538225,35.253779615],[113.1741425,35.25142427],[113.175230105,35.251488235],[113.17747017,35.2515036],[113.179628475,35.250914735],[113.180360285,35.25044944],[113.181109635,35.24898366],[113.181509125,35.247514605],[113.18180451,35.244828325],[113.179187835,35.244680635],[113.17887218,35.24429662],[113.17761644,35.241702],[113.17775472,35.241155985],[113.180462015,35.23853593],[113.181767035,35.238742355],[113.18278827,35.23786684],[113.18356159,35.2350261],[113.18380546,35.234130215],[113.184470685,35.230981275],[113.184724655,35.229736275],[113.18482989,35.229156875],[113.18477688,35.22862779],[113.184187925,35.228137035],[113.18296161,35.22712901],[113.181982475,35.22639289],[113.17961477,35.22468514],[113.178471005,35.22389752],[113.17746822,35.2232288],[113.17613421,35.22238595],[113.175893305,35.22202763],[113.171988405,35.21602747],[113.172337705,35.21288546],[113.178154555,35.21418154],[113.18037235,35.216148465],[113.18407711,35.218464815],[113.187261275,35.21941073],[113.188449395,35.21738833],[113.18838572,35.217364625],[113.18834026,35.217352035],[113.187700205,35.21712832],[113.18690353,35.21684601],[113.18656684,35.21672491],[113.186485125,35.21669552],[113.18550492,35.216340855],[113.18529222,35.21626263],[113.18483851,35.2160707],[113.184630795,35.215982835],[113.18399221,35.2157127],[113.183496165,35.21550265],[113.181597735,35.21469874],[113.182103465,35.211350955],[113.1871358,35.211887255],[113.19114244,35.21018075],[113.19356786,35.208085565],[113.194425835,35.207344365],[113.199345035,35.2039673],[113.20278519,35.20408186],[113.20305078,35.2040797],[113.20385673,35.203940215],[113.204933475,35.19732813],[113.202418735,35.194907145],[113.198749485,35.19458594],[113.19890386,35.19355835],[113.199526305,35.19027924],[113.199843265,35.188703765],[113.201121955,35.182347355],[113.18474748,35.17825061],[113.184358645,35.17868251],[113.182697925,35.1805271],[113.1755536,35.181605625],[113.174969745,35.18214039],[113.167769325,35.18873462],[113.166296785,35.190530215],[113.166283265,35.190546705],[113.164574865,35.192629785],[113.164487625,35.192997625],[113.164265525,35.194132295],[113.164045935,35.19462991],[113.15795242,35.194875365],[113.155982755,35.194634795],[113.15558256,35.194154185],[113.15435023,35.18955328],[113.1544356,35.18887317],[113.154764665,35.18666192],[113.157045645,35.186960595],[113.157184895,35.18538889],[113.157079435,35.184330475],[113.155603905,35.18420832],[113.147959655,35.18425937],[113.14414643,35.184623845],[113.14314176,35.184726465],[113.142558305,35.186115165],[113.14061367,35.18780362],[113.13708313,35.190092605],[113.13242227,35.189665855],[113.12906066,35.190247035],[113.128462075,35.190350525],[113.118552025,35.198594185],[113.11379108,35.204449395],[113.107513435,35.214072705],[113.10663284,35.215675215],[113.1065348,35.216292855],[113.106400765,35.217137245],[113.10643339,35.221879745],[113.107286655,35.22965312],[113.1076357,35.231865015],[113.107937715,35.233778795],[113.10446363,35.23636276],[113.1032333,35.238343005],[113.102267915,35.239896755],[113.10031,35.243047725],[113.098589215,35.245816845],[113.09793661,35.246866975],[113.09559186,35.24716209],[113.095071275,35.24702266],[113.09447535,35.246969365],[113.08967266,35.247179655],[113.089315815,35.247376675],[113.089015545,35.24776356],[113.087518185,35.250864265],[113.087396595,35.25116324],[113.0861009,35.25608414],[113.086106335,35.256296105],[113.086187665,35.25644315],[113.08655108,35.256500495],[113.08823078,35.256471505],[113.088855875,35.25665157],[113.089481505,35.25685282],[113.089848735,35.257058535],[113.090086755,35.257266475],[113.090170165,35.25749407],[113.092583465,35.26407841],[113.094535715,35.269404055],[113.094595055,35.2696999],[113.094913205,35.27100915],[113.097108775,35.275933235],[113.097272615,35.27626969],[113.09756514,35.276582705],[113.09793302,35.276809585],[113.098349325,35.27690839],[113.10257768,35.279422045],[113.107763725,35.286944585],[113.11445707,35.29768499],[113.11467658,35.298168875],[113.11487081,35.29867441],[113.11503752,35.29911682],[113.1174143,35.30874195],[113.117524535,35.30918833],[113.11876969,35.31427704],[113.118863855,35.314911555],[113.122779425,35.320554395],[113.12475174,35.323396415],[113.12896943,35.329323325],[113.129233075,35.32950953],[113.13081248,35.33151741],[113.131675225,35.332838135],[113.131653945,35.333390955],[113.131591835,35.33500441],[113.131523935,35.336768365],[113.13157414,35.33682162],[113.131923405,35.33721657],[113.132233785,35.337707855],[113.13303572,35.33899268],[113.133121645,35.33925343],[113.13299074,35.34301075],[113.13296316,35.343393835],[113.13294545,35.34350837],[113.13281927,35.34383394],[113.13264715,35.34414501],[113.132247065,35.344394085],[113.132088175,35.344478615],[113.131784725,35.34460969],[113.13099922,35.34479014],[113.130787545,35.344910165],[113.1306322,35.34512978],[113.130355015,35.34582302],[113.130000615,35.346935645],[113.12999918,35.34731914],[113.130150045,35.34765908],[113.13037369,35.34799459],[113.130557385,35.348123365],[113.135040285,35.349253315],[113.135809635,35.349447215],[113.138516925,35.350129495],[113.141710395,35.350474895],[113.14357208,35.35067621],[113.145494155,35.34938411],[113.14553798,35.349354645],[113.14702728,35.348353425],[113.147556115,35.348019425],[113.14761207,35.347995245],[113.148456345,35.34781793],[113.14876625,35.347766055],[113.15523804,35.34744211],[113.155718885,35.34743353],[113.155775455,35.34743252],[113.155832025,35.347431515],[113.171060485,35.35084261],[113.173084465,35.351295805],[113.17410144,35.35160434],[113.17436593,35.351684575],[113.175652675,35.35207493],[113.17676591,35.352412635],[113.17813467,35.352827835],[113.1817884,35.35399073],[113.183195045,35.35473043],[113.186901845,35.3518935],[113.18816029,35.350930305],[113.192569345,35.347555395],[113.194147075,35.34634762],[113.197957335,35.34284694],[113.19842495,35.34235154],[113.200512185,35.33897487],[113.200539205,35.33892801],[113.200593255,35.338834285],[113.20088801,35.338226095],[113.20091408,35.33814448],[113.200940165,35.338062845],[113.20238709,35.33434598],[113.205041525,35.327526345],[113.205529135,35.32679924],[113.207141545,35.325030975],[113.208718065,35.32364135],[113.209704505,35.322602705],[113.210367195,35.321532165],[113.210928405,35.32012328],[113.2115182,35.318071255],[113.21146696,35.31788319],[113.211186265,35.31773713],[113.21025919,35.317565075],[113.20836408,35.31741072],[113.207992225,35.31730411],[113.20642279,35.315556145],[113.206181015,35.315144745],[113.206071385,35.31450414],[113.205977195,35.31361327]]]]}";
        JSONObject jsonObject = JSON.parseObject(str);
        //解析jsonObject
        String type = jsonObject.get("type").toString();//获取类型:MultiPolygon
        String coordinates=jsonObject.get("coordinates").toString();//获取坐标信息：coordinates
        System.out.println(coordinates.replace("[","").replace("]",""));

    }

    @Test
    public void test2(){
        System.out.println(getHeatMapJsonFileContent());
    }

}
